<?php
class Core_Db_Adapter_Mysql extends Core_Db_Adapter_Abstract{

    protected function _conectar()
    {
        $this->_conexion = mysql_connect($this->_config['host'],
                            $this->_config['usuario'],
                            $this->_config['clave']);
        if (!$this->_conexion)
        {
            throw new Exception("Error al conectar con BD", 0);
        }

        mysql_select_db($this->_config['dbname'], $this->_conexion);
        $this->_isConectado = mysql_ping($this->_conexion);
    }

    protected function _desconectar()
    {
        mysql_close($this->_conexion);
        $this->_isConectado = @mysql_ping($this->_conexion);
    }

    public function select($cols=null)
    {
        $this->_conectar();
        $resultado = array();

        $query = "SELECT ";

        if ($cols == null)
        {
            $query .= " * ";
        }

        if (!is_array($cols)){
            $query .= " ".$cols." ";
        }else{
            for ($i=0;$i<count($cols)-1;$i++)
            {
                $query .= " ".$cols[$i].",";
            }
            $query .= " ".$cols[count($cols)-1];
        }

        $query .=" FROM ".$this->_table;

        if (strlen($this->_where)>0)
        {
            $query .= $this->_where;
            if (strlen($this->_notWhere)>0){
                $query .= " and ".$this->_notWhere;
            }
        }else{
            if (strlen($this->_notWhere)>0){
                $query .= " WHERE ".$this->_notWhere;
            }
        }

        if (strlen($this->_order)>0)
        {
            $query .= $this->_order;
        }

        if (strlen($this->_limit)>0)
        {
            $query .= $this->_limit;
        }
        
        $resultset = mysql_query($query,$this->_conexion);

        if (!$resultset)
        {
            echo $query;
            throw new Exception("No se pudo ejecutar la consulta.", 0);
        }

        if (mysql_num_rows($resultset) == 0)
        {
            throw new Exception("No se encontro ninguna fila.", 0);
        }

        while ($row = mysql_fetch_object($resultset))
        {
            $resultado[]=$row;
        }
        
        mysql_free_result($resultset);
        $this->_desconectar();
        
        return $resultado;
    }
    
    public function insert($cols,$vals)
    {
        $this->_conectar();
        $query = "INSERT INTO ".$this->_table." (";
        
        if (!is_array($cols) && !is_array($vals))
        {
            if (is_string($vals))
            {
               $query .= $cols.") VALUES ('".$vals."')";
            }else{
               $query .= $cols.") VALUES (".$vals.")";
            }
        }else{
            if (count($cols)!=count($vals))
            {
                throw new Exception("Columnas y valores de diferente tamaño.", 0);
                return false;
            }

            $columnas = "";
            $valores = "";

            for ($i=0;$i<count($cols)-1;$i++)
            {
                $columnas .= $cols[$i].",";
                if (is_string($vals[$i]))
                {
                    $valores .= "'".$vals[$i]."',";
                }else{
                    $valores .= $vals[$i].",";
                }
            }
            $columnas .= $cols[count($cols)-1];
            if (is_string($vals[count($vals)-1]))
            {
                $valores .= "'".$vals[count($vals)-1]."'";
            }else{
                $valores .= $vals[count($vals)-1];
            }
            $query .= $columnas.") VALUES (".$valores.")";
        }
        
        $resultado = mysql_query($query, $this->_conexion);
        $this->_desconectar();
        return $resultado;
    }

    public function update($cols,$vals)
    {
    }

    public function delete()
    {
    }

    public function setWhere($cols,$vals)
    {
        if (!is_array($cols))
        {
            $this->_where = " WHERE ".$cols."=".$vals;
            return;
        }

        if (!count($cols) == count($vals))
        {
            throw new Exception("El tamaño de los arreglos no es el mismo.", 0);
        }

        $this->_where = " WHERE ";
        for ($i=0;$i<(count($cols)-1);$i++)
        {
           $this->_where .= $cols[$i]."='".$vals[$i]."' and ";
        }

        $this->_where .= $cols[count($cols)-1]."='".$vals[count($cols)-1]."'";
    }

    public function setNotWhere($cols,$vals)
    {
        if (!is_array($cols))
        {
            $this->_notWhere = cols."!=".$vals;
            return;
        }

        if (!count($cols) == count($vals))
        {
            throw new Exception("El tamaño de los arreglos no es el mismo.", 0);
        }

        for ($i=0;$i<(count($cols)-1);$i++)
        {
           $this->_notWhere .= $cols[$i]."!=".$vals[$i]." and ";
        }

        $this->_notWhere .= $cols[count($cols)-1]."!=".$vals[count($cols)-1];
    }
    
    public function setLimit($limit)
    {
        $this->_limit = " LIMIT ".$limit;
    }

    public function setOrder($orderBy,$order=null)
    {
        $this->_order = " ORDERBY ".$orderBy." ".$order;
    }
}
//Archivo: Mysql.php
//Ubicacion: Core/Db/Adapter/Mysql.php